﻿using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Forms;

namespace HuiZe.education.View
{
    /// <summary>
    /// UploadView.xaml 的交互逻辑
    /// </summary>
    public partial class UploadView : HuiZeWindow
    {
        public bool WaitingLoadData { get; set; }

        public UploadView()
        {
            InitializeComponent();
            this.MaxHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height + 10;
            this.MaxWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width + 10;
            Loaded += UploadView_Loaded;
        }

        void UploadView_Loaded(object sender, RoutedEventArgs e)
        {
            MySpaceViewModel mySpaceViewModel = DataContext as MySpaceViewModel;
            mySpaceViewModel.v_upload_mask = Visibility.Collapsed;
            if (WaitingLoadData)
            {
                new Thread(new ThreadStart(() =>
                {
                    mySpaceViewModel.spaceUseInfo();
                    Dispatcher.Invoke(new System.Action(() =>
                    {
                        mySpaceViewModel.GetGroupList(1);
                        mySpaceViewModel.upload_size_total = 1;
                        mySpaceViewModel.upload_size_value = 0;
                        mySpaceViewModel.upload_file_name = string.Empty;
                        mySpaceViewModel.SelectedUploadGroup = mySpaceViewModel.MyGroups_Upload[0];
                    }));
                }))
                { IsBackground = true }.Start();
            }
            else
            {
                mySpaceViewModel.upload_size_total = 1;
                mySpaceViewModel.upload_size_value = 0;
                mySpaceViewModel.upload_file_name = string.Empty;
                mySpaceViewModel.SelectedUploadGroup = mySpaceViewModel.MyGroups_Upload[0];
            }
        }

        private void YCButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = new bool?(false);
        }

        private void YCButton_Click_1(object sender, RoutedEventArgs e)
        {

            //MySpaceViewModel mySpaceViewModel = DataContext as MySpaceViewModel;
            //if (string.IsNullOrEmpty(mySpaceViewModel.current_attachment_id))
            //{
            //    mySpaceViewModel.ShowDialogView("请选择需要上传的文件!");
            //    return;
            //}

            //new Thread(new ThreadStart(() =>
            //{
            //    if (mySpaceViewModel.uploadFile_Complete())
            //    {
            //        Dispatcher.Invoke(new System.Action(() =>
            //        {
            //            this.DialogResult = new bool?(true);
            //        }));
            //    }
            //}))
            //{ IsBackground = true }.Start();

            //ShowDialogResult = true;
            //Close();
        }

        void CustomButton_Click(object sender, RoutedEventArgs e)
        {
            string fileName = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件";
            dialog.Filter = "文件(*.*)";
            //if (dialog.ShowDialog()==DialogResult.ok)
            //{
            //    fileName = dialog.FileName;
            //    FileInfo fileInfo = new FileInfo(dialog.FileName);
            //    if (fileLenght > 0 && fileInfo.Length / 1024 > fileLenght)
            //    {
            //        MessageBox.Show("图片超过2M，请重新选择");
            //        return "";
            //    }
            //}
            //return fileName;
        }

        void CustomButton_Click_1(object sender, RoutedEventArgs e)
        {
            MySpaceViewModel mySpaceViewModel = DataContext as MySpaceViewModel;
            new Thread(new ThreadStart(() =>
            {
                if (mySpaceViewModel.uploadFile_background())
                {
                    Dispatcher.Invoke(new System.Action(() =>
                    {
                        mySpaceViewModel.ShowDialogView("上传成功!");
                    }));
                    
                    if (mySpaceViewModel.uploadFile_Complete())
                    {
                        Dispatcher.Invoke(new System.Action(() =>
                        {
                            this.DialogResult = new bool?(true);
                        }));
                    }
                    else
                    {
                        Dispatcher.Invoke(new System.Action(() =>
                        {
                            mySpaceViewModel.ShowDialogView("上传失败!");
                        }));
                    }
                }
            }))
            { IsBackground = true }.Start();
            //if (mySpaceViewModel.uploadFile())
            //    this.DialogResult = new bool?(true);
        }
    }
}
